﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Security;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Forms;

using Microsoft.Win32;

using Microsoft.SharePoint.Administration;
using System.Configuration;
using System.IO;
using SharepointSolutionInstaller.Resources;

namespace SharepointSolutionInstaller.Utils.RequiredChecks
{

    /// <summary>
    /// Base class for all system checks.
    /// </summary>
    public abstract class SystemCheck
    {
        private readonly string id;
        private string questionText;
        private string okText;
        private string errorText;

        protected SystemCheck(string id)
        {
            this.id = id;
        }

        public string Id
        {
            get { return id; }
        }

        public string QuestionText
        {
            get { return questionText; }
            set { questionText = value; }
        }

        public string OkText
        {
            get { return okText; }
            set { okText = value; }
        }

        public string ErrorText
        {
            get { return errorText; }
            set { errorText = value; }
        }

        internal SystemCheckResult Execute()
        {
            if (CanRun)
            {
                return DoExecute();
            }
            return SystemCheckResult.Inconclusive;
        }

        protected abstract SystemCheckResult DoExecute();

        protected virtual bool CanRun
        {
            get { return true; }
        }

        protected static bool IsWSSInstalled
        {
            get
            {
                string name = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0";

                try
                {
                    RegistryKey key = Registry.LocalMachine.OpenSubKey(name);
                    if (key != null)
                    {
                        object val = key.GetValue("SharePoint");
                        if (val != null && val.Equals("Installed"))
                        {
                            return true;
                        }
                    }
                    return false;
                }

                catch (SecurityException ex)
                {
                    throw new Exception(string.Format(Resources.CommonUIStrings.installExceptionAccessDenied, name), ex);
                }
            }
        }

        protected static bool IsMOSSInstalled
        {
            get
            {
                string name = @"SOFTWARE\Microsoft\Office Server\14.0";

                try
                {
                    RegistryKey key = Registry.LocalMachine.OpenSubKey(name);
                    if (key != null)
                    {
                        string versionStr = key.GetValue("BuildVersion") as string;
                        if (versionStr != null)
                        {
                            Version buildVersion = new Version(versionStr);
                            if (buildVersion.Major == 14)
                            {
                                return true;
                            }
                        }
                    }
                    return false;
                }

                catch (SecurityException ex)
                {
                    throw new Exception(string.Format(CommonUIStrings.installExceptionAccessDenied, name), ex);
                }
            }
        }
    }   

}
